package com.algrithom.array;

import java.util.Arrays;

/**
 * 88. 合并两个有序数组
 * 
 * @author think
 */
public class Solution3 {
    
    public static void main(String[] args){
        int[] nums1 = new int[]{0};
        int[] nums2 = new int[]{1};
        Solution3 solution3 = new Solution3();
        solution3.merge(nums1,0,nums2,1);
        System.out.println(Arrays.toString(nums1));
    }
    
    public void merge(int[] nums1,int m,int[] nums2,int n){
        int[] tmp = new int[m + n];
        int i = 0;
        int j = 0;
        int k = 0;
        while (i < m && j < n) {
            if (nums1[i] < nums2[j]) {
                tmp[k++] = nums1[i];
                i++;
            } else {
                tmp[k++] = nums2[j];
                j++;
            }
        }
        while (i < m) {
            tmp[k++] = nums1[i++];
        }
        while (j < n) {
            tmp[k++] = nums2[j++];
        }
        System.arraycopy(tmp,0,nums1,0,m + n);
    }
}
